<?php

namespace Eduxplus\CoreBundle\Twig;

use Eduxplus\CoreBundle\Lib\Service\HelperService;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class FiltersExtension extends AbstractExtension
{
    protected $helperService;

    public function __construct(HelperService $helperService)
    {
        $this->helperService = $helperService;
    }

    public function getFilters()
    {
        return [
            new TwigFilter('substr', [$this, 'substr']),
            new TwigFilter('html2txt', [$this, 'html2txt']),
            new TwigFilter('qtime', [$this, 'qtime']),
            new TwigFilter('addslashes', [$this, 'addslashes']),
        ];
    }

    public function addslashes($string){
        return addslashes($string);
    }
    
    public function qtime($time)
    {
        $str = $time+0;
        if($str != $time){
            $time = strtotime($time);
        }
        return $this->helperService->qtime($time);
    }

    public function substr($string , $start, $length=null)
    {

        return mb_substr($string , $start, $length, "utf-8");
    }

    public function html2txt($string)
    {
        return strip_tags($string);
    }
}
